﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Threading;
using System.Messaging;

namespace MSMQWinform
{
    //消息提示
    public delegate void MessageDialog(string msg);
    //收到的队列消息
    public delegate void ReceiveMessage(Object msg);

    public class ThreadManager : IDisposable
    {
        #region Attrible
        private static int m_ThreadNumber = 5;

        private static Thread[] m_ThreadArray = new Thread[m_ThreadNumber];

        private MessageDialog m_MessageDialog;

        public MessageDialog MessageDialog
        {
            get { return m_MessageDialog; }
            set { m_MessageDialog = value; }
        }

        private ReceiveMessage m_ReceiveMessage;

        public ReceiveMessage ReceiveMessage
        {
            get { return m_ReceiveMessage; }
            set { m_ReceiveMessage = value; }
        }

        private string m_QueuePath;

        public string QueuePath
        {
            get { return m_QueuePath; }
            set { m_QueuePath = value; }
        }
        #endregion

        public static object m_LookObject = new object();
        public static ThreadManager m_ThreadManager = null;

        public static ThreadManager Instance
        {
            get
            {
                lock (m_LookObject)
                {
                    if (m_ThreadManager == null)
                    {
                        m_ThreadManager = new ThreadManager();
                    }
                }
                return m_ThreadManager;
            }
        }

        public void ThreadStart()
        {
            for (int i = 0; i < m_ThreadNumber; i++)
            {
                m_ThreadArray[i] = new Thread(new ParameterizedThreadStart(MSMQListen));
                m_ThreadArray[i].IsBackground = true;
                m_ThreadArray[i].Start(i);
                MessageDialog("[" + i.ToString() + "] 号线程开始监听!");
            }
        }

        public void ThreadStop()
        {
            for (int i = 0; i < m_ThreadNumber; i++)
            {
                if (m_ThreadArray[i] != null && m_ThreadArray[i].ThreadState != ThreadState.Stopped)
                {
                    m_ThreadArray[i].Abort();
                }
            }
        }

        private void MSMQListen(object index)
        {
            while (true)
            {
                try
                {
                    MessageDialog("[" + index.ToString() + "]号线程监听消息中...");
                    Message m_Receive = QueueManager.ReceiveMsg<SendMsg>(QueuePath, true);// 这里通过线程阻塞来监视队列
                    MessageDialog("[" + index.ToString() + "]号线程收到消息>>>");
                    ReceiveMessage(m_Receive);
                }
                catch (Exception ex)
                {
                    MessageDialog(ex.Message);
                }
            }
        }

        #region IDisposable 成员

        public void Dispose()
        {
        }

        #endregion
    }
}
